/**
 * @project lauwsys
 */
package com.chimoz.lauwsys.business.repository.jpa;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.chimoz.lauwsys.domain.Brand;
import com.chimoz.lauwsys.repository.BrandRepository;

/**
 * @author Christian Teguh
 * @created 13 Okt 2011 19.11.47
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/db/lauwsys-jpa.xml"})
@TransactionConfiguration
@Transactional
public class PBrandRepositoryTestCase extends AbstractTransactionalJUnit4SpringContextTests {

	@Autowired
	private BrandRepository brandRepository;
	
	
	public void getBrandById() {
		Long id = new Long(2);
		Brand brand = this.brandRepository.getBrandById(id);
		assertNotNull(brand);
		assertEquals( brand.getId(), id );
        System.out.println(brand);
	}
	
	public void insertBrand() {
		Brand newBrand = new Brand();
//		newBrand.setId(new Long(6));
		newBrand.setName("Test2");
		newBrand.setDesc("Test5");
		newBrand.setCtime((int) (System.currentTimeMillis() / 1000L));
		newBrand.setMtime((int) (System.currentTimeMillis() / 1000L));
		Brand brand = this.brandRepository.saveBrand(newBrand);
//		assertNotNull(brand);
		assertTrue(brand.getId() > 0);
		System.out.println(brand.getId());
	}
	
	
	public void updateBrand() {
		Brand newBrand = new Brand();
		newBrand.setId(new Long(6));
		newBrand.setName("Test1");
		newBrand.setDesc("Test6");
		Brand brand = this.brandRepository.saveBrand(newBrand);
		assertNotNull(brand);
		Brand testBrand = this.brandRepository.getBrandById(new Long(6));
		assertEquals( testBrand.getDesc(), brand.getDesc() );
		assertEquals( testBrand.getName(), brand.getName() );
        System.out.println(brand.getDesc());
	}
	
	@Test
	public void getAllBrandList() {
		
		Integer limit = new Integer(10);
		
		Map<String, String> criteria = new HashMap<String, String>();
		criteria.put("limit", limit.toString());
		criteria.put("offset", "20" );
		List<Brand> brands = this.brandRepository.listBrand(criteria);
		
		assertEquals( brands.size(), limit.intValue() );
	}
}

